#include "etcd.hpp"
#include "channel.hpp"
#include <gflags/gflags.h>
#include <gtest/gtest.h>
#include <thread>
#include "friend.pb.h"
#include "utils.hpp"

DEFINE_bool(run_mode, false, "程序的运行模式, false-调试模式, true-发布模式");
DEFINE_string(log_file, "", "发布模式下, 用于指定日志文件的路径");
DEFINE_int32(log_level, 0, "发布模式下, 用于指定日志的输出等级");

DEFINE_string(etcd_host, "http://127.0.0.1:2379", "这是服务注册中心的地址");
DEFINE_string(base_service, "/service", "这是服务监控根目录");
DEFINE_string(friend_service, "/service/friend_service", "这是服务监控根目录");

suye::ServiceManager::ptr sm;

void apply_test(const std::string& uid1, const std::string& uid2) 
{
    auto channel = sm->Choose(FLAGS_friend_service);
    if (!channel) {
        std::cout << "获取通信信道失败！" << std::endl;
        return;
    }
    suye::FriendService_Stub stub(channel.get());
    suye::FriendAddReq req;
    suye::FriendAddRsp rsp;
    brpc::Controller cntl;
    req.set_request_id(suye::uuid());
    req.set_user_id(uid1);
    req.set_respondent_id(uid2);
    stub.FriendAdd(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
}

void get_apply_list(const std::string& uid) 
{
    auto channel = sm->Choose(FLAGS_friend_service);
    if (!channel) {
        std::cout << "获取通信信道失败！" << std::endl;
        return;
    }
    suye::FriendService_Stub stub(channel.get());
    suye::GetPendingFriendEventListReq req;
    suye::GetPendingFriendEventListRsp rsp;
    brpc::Controller cntl;
    req.set_request_id(suye::uuid());
    req.set_user_id(uid);
    stub.GetPendingFriendEventList(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
    for (int i = 0; i < rsp.event_size(); i++) {
        std::cout << "---------------\n";
        std::cout << rsp.event(i).sender().user_id() << std::endl;
        std::cout << rsp.event(i).sender().nickname() << std::endl;
        std::cout << rsp.event(i).sender().avatar() << std::endl;
    }
}

void process_apply_test(const std::string& uid, bool agree, const std::string& apply_user_id) {
    auto channel = sm->Choose(FLAGS_friend_service);
    if (!channel) {
        std::cout << "获取通信信道失败！" << std::endl;
        return;
    }
    suye::FriendService_Stub stub(channel.get());
    suye::FriendAddProcessReq req;
    suye::FriendAddProcessRsp rsp;
    req.set_request_id(suye::uuid());
    req.set_user_id(uid);
    req.set_agree(agree);
    req.set_apply_user_id(apply_user_id);
    brpc::Controller cntl;
    stub.FriendAddProcess(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
    if (agree) {
        std::cout << rsp.new_session_id() << std::endl;
    }
}

void search_test(const std::string& uid, const std::string& key) {
    auto channel = sm->Choose(FLAGS_friend_service);
    if (!channel) {
        std::cout << "获取通信信道失败！" << std::endl;
        return;
    }
    suye::FriendService_Stub stub(channel.get());
    suye::FriendSearchReq req;
    suye::FriendSearchRsp rsp;
    req.set_request_id(suye::uuid());
    req.set_user_id(uid);
    req.set_search_key(key);
    brpc::Controller cntl;
    stub.FriendSearch(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
    for (int i = 0; i < rsp.user_info_size(); i++) {
        std::cout << "-------------------\n";
        std::cout << rsp.user_info(i).user_id() << std::endl;
        std::cout << rsp.user_info(i).nickname() << std::endl;
        std::cout << rsp.user_info(i).avatar() << std::endl;
    }
}

void friend_list_test(const std::string &uid) {
    auto channel = sm->Choose(FLAGS_friend_service);
    if (!channel) {
        std::cout << "获取通信信道失败！" << std::endl;
        return;
    }
    suye::FriendService_Stub stub(channel.get());
    suye::GetFriendListReq req;
    suye::GetFriendListRsp rsp;
    req.set_request_id(suye::uuid());
    req.set_user_id(uid);
    brpc::Controller cntl;
    stub.GetFriendList(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
    for (int i = 0; i < rsp.friend_list_size(); i++) {
        std::cout << "-------------------\n";
        std::cout << rsp.friend_list(i).user_id() << std::endl;
        std::cout << rsp.friend_list(i).nickname() << std::endl;
        std::cout << rsp.friend_list(i).avatar() << std::endl;
    }
}

void remove_test(const std::string &uid1, const std::string &uid2) {
    auto channel = sm->Choose(FLAGS_friend_service);
    if (!channel) {
        std::cout << "获取通信信道失败！" << std::endl;
        return;
    }
    suye::FriendService_Stub stub(channel.get());
    suye::FriendRemoveReq req;
    suye::FriendRemoveRsp rsp;
    req.set_request_id(suye::uuid());
    req.set_user_id(uid1);
    req.set_peer_id(uid2);
    brpc::Controller cntl;
    stub.FriendRemove(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
}

void create_css_test(const std::string& uid, const std::vector<std::string>& uidlist) {
    auto channel = sm->Choose(FLAGS_friend_service);
    if (!channel) {
        std::cout << "获取通信信道失败！" << std::endl;
        return;
    }
    suye::FriendService_Stub stub(channel.get());
    suye::ChatSessionCreateReq req;
    suye::ChatSessionCreateRsp rsp;
    req.set_request_id(suye::uuid());
    req.set_user_id(uid);
    req.set_chat_session_name("快乐一家人");
    for (auto &id : uidlist) {
        req.add_member_id_list(id);
    }
    brpc::Controller cntl;
    stub.ChatSessionCreate(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
    std::cout << rsp.chat_session_info().chat_session_id() << std::endl;
    std::cout << rsp.chat_session_info().chat_session_name() << std::endl;
}

void cssmember_test(const std::string& uid, const std::string& cssid) {
    auto channel = sm->Choose(FLAGS_friend_service);
    if (!channel) {
        std::cout << "获取通信信道失败！" << std::endl;
        return;
    }
    suye::FriendService_Stub stub(channel.get());
    suye::GetChatSessionMemberReq req;
    suye::GetChatSessionMemberRsp rsp;
    req.set_request_id(suye::uuid());
    req.set_user_id(uid);
    req.set_chat_session_id(cssid);
    brpc::Controller cntl;
    stub.GetChatSessionMember(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
    for (int i = 0; i < rsp.member_info_list_size(); i++) {
        std::cout << "-------------------\n";
        std::cout << rsp.member_info_list(i).user_id() << std::endl;
        std::cout << rsp.member_info_list(i).nickname() << std::endl;
        std::cout << rsp.member_info_list(i).avatar() << std::endl;
    }
}

void csslist_test(const std::string& uid) {
    auto channel = sm->Choose(FLAGS_friend_service);
    if (!channel) {
        std::cout << "获取通信信道失败！" << std::endl;
        return;
    }
    suye::FriendService_Stub stub(channel.get());
    suye::GetChatSessionListReq req;
    suye::GetChatSessionListRsp rsp;
    req.set_request_id(suye::uuid());
    req.set_user_id(uid);
    brpc::Controller cntl;
    std::cout << "发送获取聊天会话列表请求！！\n";
    stub.GetChatSessionList(&cntl, &req, &rsp, nullptr);
    std::cout << "请求发送完毕1！！\n";
    ASSERT_FALSE(cntl.Failed());
    std::cout << "请求发送完毕2！！\n";
    ASSERT_TRUE(rsp.success());
    std::cout << "请求发送完毕，且成功！！\n";
    for (int i = 0; i < rsp.chat_session_info_list_size(); i++) {
        std::cout << "-------------------\n";
        std::cout << rsp.chat_session_info_list(i).single_chat_friend_id() << std::endl;
        std::cout << rsp.chat_session_info_list(i).chat_session_id() << std::endl;
        std::cout << rsp.chat_session_info_list(i).chat_session_name() << std::endl;
        std::cout << rsp.chat_session_info_list(i).avatar() << std::endl;
        std::cout << "消息内容：\n";
        std::cout << rsp.chat_session_info_list(i).prev_message().message_id() << std::endl;
        std::cout << rsp.chat_session_info_list(i).prev_message().chat_session_id() << std::endl;
        std::cout << rsp.chat_session_info_list(i).prev_message().timestamp() << std::endl;
        std::cout << rsp.chat_session_info_list(i).prev_message().sender().user_id() << std::endl;
        std::cout << rsp.chat_session_info_list(i).prev_message().sender().nickname() << std::endl;
        std::cout << rsp.chat_session_info_list(i).prev_message().sender().avatar() << std::endl;
        std::cout << rsp.chat_session_info_list(i).prev_message().message().file_message().file_name() << std::endl;
        std::cout << rsp.chat_session_info_list(i).prev_message().message().file_message().file_contents() << std::endl;
    }
}

int main(int argc, char *argv[])
{
    google::ParseCommandLineFlags(&argc, &argv, true);
    suye::Init_logger(FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);

    // 1.先构造Rpc信道管理对象
    sm = std::make_shared<suye::ServiceManager>();
    sm->Declared(FLAGS_friend_service);
    auto put_cb = std::bind(&suye::ServiceManager::OnServiceOnline, sm.get(), std::placeholders::_1, std::placeholders::_2);
    auto del_cb = std::bind(&suye::ServiceManager::OnServiceOffline, sm.get(), std::placeholders::_1, std::placeholders::_2);
    // 2.构造服务发现对象
    suye::Discovery::ptr dclient = std::make_shared<suye::Discovery>(FLAGS_etcd_host, FLAGS_base_service, put_cb, del_cb);

    // apply_test("c607-b84d2c84-0003", "c6e2-5a2e4533-0002");
    // apply_test("9804-d31f8fe7-0004", "c6e2-5a2e4533-0002");
    // apply_test("0240-fc70fa0f-0005", "c607-b84d2c84-0003");

    // get_apply_list("c6e2-5a2e4533-0002");

    // process_apply_test("c6e2-5a2e4533-0002", true, "c607-b84d2c84-0003");
    // process_apply_test("c6e2-5a2e4533-0002", false, "9804-d31f8fe7-0004");
    // process_apply_test("c607-b84d2c84-0003", true, "0240-fc70fa0f-0005");

    // search_test("c6e2-5a2e4533-0002", "猪");
    // std::cout << "++++++++++++++++++++++" << std::endl;
    // search_test("c607-b84d2c84-0003", "猪");

    // friend_list_test("c607-b84d2c84-0003");
    // std::cout << "++++++++++++++++++++++" << std::endl;
    // friend_list_test("9804-d31f8fe7-0004");

    // remove_test("c607-b84d2c84-0003", "0240-fc70fa0f-0005");

    // std::vector<std::string> uid_list = {
    //                 "c6e2-5a2e4533-0002", 
    //                 "c607-b84d2c84-0003",
    //                 "9804-d31f8fe7-0004",
    //                 "0240-fc70fa0f-0005"};               
    // create_css_test("c6e2-5a2e4533-0002", uid_list);

    // cssmember_test("c6e2-5a2e4533-0002", "dafd-dccf9602-0000");

    csslist_test("c6e2-5a2e4533-0002");

    return 0;
}